import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken, removeToken } from './mUtils'

NProgress.configure({ showSpinner: false })

const whiteList = ['/home'] // 不重定向的白名单

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (whiteList.indexOf(to.path) !== -1) {
    next()
    NProgress.done()
  } else if (to.path === '/login') {
    next()
    removeToken()
    NProgress.done()
  } else {
    if (getToken()) {
      next()
      NProgress.done()
    } else {
      next({ path: '/login' })
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 完成进度条
})
